<template>
  <div class="live">
    <section class="search-content">
      <el-row :gutter="4" type="flex" justify="start">
        <el-col :span="24">
          <el-input v-model="listQuery.title" placeholder="课程名称" style="width:220px;" @keyup.enter.native="handleFilter"/>
          <el-date-picker
            v-model="listQuery.dateTime"
            type="datetimerange"
            range-separator="|"
            start-placeholder="起始时间"
            end-placeholder="直播结束"
            @change="handleFilter"
          />
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button size="medium" type="info" @click="clearParams">清空</el-button>
          <el-select v-model="listQuery.status" style="width:150px;" @change="handleFilter">
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-col>
      </el-row>
    </section>
    <subtable
      :table-label="zeroLabel"
      :table-data="zeroData.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    />

    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
  import Subtable from '@/components/Eltable'
  import Pagination from '@/components/Pagination' // 分页组件
  import {getLiveList} from '@/api/subject/live'
  import {getTenantId} from '@/utils/auth'

  export default {
    components: {
      Pagination,
      Subtable
    },
    data() {
      return {
        total: 0,
        currentpage: 1,
        pagesize: 10,
        listFilter: [],
        listQuery: {
          dateTime: '',
          subject: '',
          title: '',
          status: ''
        },
        status: [
          {
            label: '即将开始的',
            value: '即将开始的'
          },
          {
            label: '当前进行中',
            value: '当前进行中'
          },
          {
            label: '已结束的',
            value: '已结束的'
          }
        ],
        zeroLabel: [
          {
            label: '任务', width: '30', prop: 'title'
          }, {
            label: '直播开始时间', width: '20', prop: 'startTimeStr'
          }, {
            label: '直播时长(分)', width: '20', prop: 'length'
          }, {
            label: '最大参与人数', width: '20', prop: 'memberNum'
          }
        ],
        zeroData: []
      }
    },
    mounted() {
      this.getData()
    },
    methods: {
      handleSizeChange(val) {
        this.currentpage = 1
        this.pagesize = val.value
        if (this.pagesize >= 20) {
          scrollTo(50, 800)
        }
      },
      // 改变当前页
      handleCurrentChange(val) {
        this.currentpage = val.value
        if (this.pagesize >= 20) {
          scrollTo(50, 800)
        }
      },
      handleFilter() {
        this.zeroData = this.listFilter.filter(item => {
          if (this.listQuery.title && item.title.toLowerCase().indexOf(this.listQuery.title.toLowerCase()) < 0) return false
          if (this.listQuery.status === '即将开始的' && new Date(item.startTime) < new Date()) return false
          if (this.listQuery.status === '当前进行中' && (new Date(item.startTime) > new Date() || new Date(item.endTime) < new Date())) return false
          if (this.listQuery.status === '已结束的' && new Date(item.endTime) > new Date()) return false
          if (this.listQuery.dateTime && (new Date(item.startTime) < this.listQuery.dateTime[0] || new Date(item.startTime) > this.listQuery.dateTime[1])) return false
          return true
        })
        this.total = this.zeroData.length
      },
      clearParams() {
        this.listQuery.dateTime = ''
        this.listQuery.subject = ''
        this.listQuery.title = ''
        this.listQuery.status = ''
        this.getData()
      },
      getData() {
        getLiveList(getTenantId()).then(response => {
          response.data.items.map(item => {
            if (item.startTime) item.startTimeStr = new Date((item.startTime)).toLocaleString().substring(0, 10).replace(/[\u4e00-\u9fa5]/g, '')
          })
          this.listFilter = response.data.items
          this.zeroData = response.data.items
          this.total = this.zeroData.length
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .live {
    margin-top: 0;
    padding: 14px 20px 21px 20px;

    .search-content {
      width: 100%;
    }
  }
</style>
